﻿@model IEnumerable<CleverCMS.Core.Services.ViewModels.SearchModel>

@{
    Layout = "~/skins/zottor/views/zottor.cshtml";
}

@section HeadContent{

}

@section TitleContent{
    Search
}

<div class="widget widget-tabs red">
    <div class="widget-title">
    </div>
    <div class="widget-body">
        <div class="tabbable portlet-tabs">

            <ul class="nav nav-tabs pull-left">
                <li class=""><a href="#portlet_tab2" data-toggle="tab" rel="posts">Entry Search (@Model.Count(x=>x.IsPage == false))</a></li>
                <li class="active"><a href="#portlet_tab1" data-toggle="tab" rel="pages">Page Search (@Model.Count(x=>x.IsPage == true))</a></li>
            </ul>
            <div class="clearfix"></div>
            <div class="tab-content">
                <div class="tab-pane active" id="portlet_tab1">
                    <form class="form-horizontal search-result">
                        <div class="control-group">
                            <label class="control-label">Search</label>
                            <div class="controls">
                                <input type="text" class="input-xxlarge">
                            </div>
                            <button class="btn">SEARCH</button>
                        </div>
                    </form>
                    <div class="space20"></div>
                    
                    @if (!Model.Any(x => x.IsPage))
                    {
                        <h4>Sorry, but not found pages!</h4>
                    }
                    else
                    {
                        foreach (var page in Model)
                        {
                            if (page.IsPage)
                            {
                                <div class="classic-search">
                                    <h4><a href="@Url.Action("Content","Home", new { area = "", name = page.Alias })">@page.Title</a></h4>
                                    @*<a href="@Url.Action("Content","Home", new { area = "", name = page.Alias })">@Html.Raw(Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port))/pages/@page.Alias</a>*@
                                    @* <p>@Html.Raw(page.)</p>*@
                                </div>    
                            }  
                        }
                    }
                </div>
                <div class="tab-pane" id="portlet_tab2">
                    <form class="form-horizontal search-result">
                        <div class="control-group">
                            <label class="control-label">Search</label>
                            <div class="controls">
                                <input type="text" class="input-xxlarge">
                            </div>
                            <button class="btn">SEARCH</button>
                        </div>
                    </form>
                    <div class="space20"></div>

                    @if (Model.All(x => x.IsPage != false))
                    {
                        <h4>Sorry, but not found entries!</h4>
                    }
                    else
                    {
                        foreach (var entry in Model)
                        {
                            if (entry.IsPage == false)
                            {
                                <div class="classic-search">
                                    <h4><a href="@Url.Action("Entry","Blog", new { area = "", category = entry.Category.ToLower(), id = entry.Alias } )">@entry.Title</a></h4>
                                    @*<a href="@Url.Action("Entry","Blog", new { area = "", category = entry.Category.ToLower(), id = entry.Alias } )">
                                        @Html.Raw(Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port) + "/blog/" + entry.Category.ToLower() + "/entry/" + entry.Alias)    
                                    </a>*@
                                   @* <p>@Html.Raw(entry.Content)</p>*@
                                </div>    
                            }  
                        }
                    }
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $('.widget-body button[class="btn"]').on('click', function (e) {
            var type = $('.nav-tabs > li.active > a').attr('rel');
            if (type == 'pages') {
                var searchedPage = $(this).parent().find('input[type=text]').val();
                $('#portlet_tab1 p:contains(' + searchedPage + ')').each(function (i, elem) {
                    var pageText = $(elem).text().replace("<span>", "").replace("</span>", "");
                    var theRegEx = new RegExp("(" + searchedPage + ")", "igm");
                    var newHtml = pageText.replace(theRegEx, "<span style='text-decoration: underline; background-color: yellow;'>$1</span>");
                    $(elem).html(newHtml);
                });
            } else if (type == 'posts') {
                var searchedPost = $(this).parent().find('input[type=text]').val();
                $('#portlet_tab2 p:contains(' + searchedPost + ')').each(function (i, elem) {
                    var pageText = $(elem).text().replace("<span>", "").replace("</span>", "");
                    var theRegEx = new RegExp("(" + searchedPost + ")", "igm");
                    var newHtml = pageText.replace(theRegEx, "<span style='text-decoration: underline; background-color: yellow;'>$1</span>");
                    $(elem).html(newHtml);
                });
            }
            e.preventDefault();
        });
    });
</script>